﻿using System;
using System.Windows.Forms;
using Main.Menus;
using Main.Substructure;
using Main.Structure;

namespace Main
{
    /// <summary>
    /// The submain-entry point for the application.
    /// </summary>
    public partial class Hub : Form
    {
        // Global variables:
        public static Configuration Configuration; // Accessible by other classes to grab configuration data
        public static Appointment prevAppt; // Accessible by other classes so it can be assigned

        // Local variables:
        private static ClientManager CM; // There should only be one of these forms at a time, so create a variable for it
        private static AppointmentManager AM; // ^


        /// <summary>
        /// Class Constructor.
        /// </summary>
        public Hub()
        {
            InitializeComponent();
            // Creates a new instance of the configuration class and passes it the directory location of where this application was ran from.
            Configuration = new Configuration(Application.StartupPath);
            Main(); // Finishes the loading code in it's own method.
        }

        /// <summary>This method contains all of the code we want to run before the form is loaded.</summary>
        private void Main()
        {
            #region Step 1 - Load Database
            // If the database failed to load, there's no point in running anymore code, so quit and notify the user.
            if (!Database.Database.Load())
            {
                Utilities.Message("Database failed to load. Please exit and debug.", "Database Failure!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            #endregion
            #region Step 2 - Preview Appointment
            if (prevAppt != null)
            {
                DateTime DT = DateTime.Today; // Today's Date for comparison.
                // Let's check to see if the appointment is today. If so, let's make it say "Today"
                if (prevAppt.Year == DT.Year && prevAppt.Month == DT.Month && prevAppt.Day == DT.Day)
                    statusItem2.Text = "Next Appointment: Today";
                else
                    statusItem2.Text = "Next Appointment: " + prevAppt.Date;
            }
            #endregion
        }

        /// <summary>
        /// This method is executed whenever the user clicks the button to run the Client Manager.
        /// It creates a new instance of the client manager and then runs it.
        /// </summary>
        private void btnClientManager_Click(object sender, EventArgs e)
        {
            // If the Client Manager window is visible, the user isn't done with it yet, so don't let
            // them accidently close it down to re-open it.
            if (CM != null && !CM.Visible)
            {
                // If the Client Manager window is NOT visible, dispose of the previous window and re-create it.
                // Then launch it.
                CM.Dispose();
                CM = new ClientManager();
                Utilities.LaunchMenu(CM);
            }
            // If the Client Manager hasn't even been created yet, create it and launch it!
            if (CM == null)
            {
                CM = new ClientManager();
                Utilities.LaunchMenu(CM);
            }
        }

        /// <summary>
        /// This method is executed whenever the user clicks the "Appointment Manager" button.
        /// This method creates a new instance of the appointment manager and then runs it.
        /// </summary>
        private void btnApptManager_Click(object sender, EventArgs e)
        {
            // If the appointment manager window has been created before and is not already open, dispose
            // of any previous appointment manager data and re-create it. Then launch it.
            if (AM != null && !AM.Visible)
            {
                AM.Dispose(); // Dispose of previous data
                AM = new AppointmentManager();
                Utilities.LaunchMenu(AM);
            }
            // If the appointment manager hasn't been created before, create it and launch it.
            if (AM == null)
            {
                AM = new AppointmentManager();
                Utilities.LaunchMenu(AM);
            }
        }
    }
}
